package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityNetworkDiagnosisBinding;
import stark.common.basic.event.EventStatProxy;

public class NetworkDiagnosisActivity extends BaseAc<ActivityNetworkDiagnosisBinding> {

    private String host;

    @Override
    protected int onCreate() {
        return R.layout.activity_network_diagnosis;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivNetworkDiagnosisBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivHostClear.setOnClickListener(this);
        mDataBinding.tvStart.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvStart://开始
                host = mDataBinding.etHost.getText().toString();
                if (TextUtils.isEmpty(host)) {
                    mDataBinding.etHost.requestFocus();
                    ToastUtils.showShort(getString(R.string.please_input_domain_ip));
                    return;
                }
                handleClickRoutingInfo();
                break;
            case R.id.tvClear://清空
                mDataBinding.etHost.setText("");
                mDataBinding.tvDiagnosisResult.setText("");
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void handleClickRoutingInfo() {
        mDataBinding.tvDiagnosisTip.setText(R.string.diagnosis_ing);
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        if (wifiInfo == null) {
            ToastUtils.showShort(getString(R.string.no_connect_wifi));
            return;
        }
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        StringBuilder builder = new StringBuilder();
        builder.append(getString(R.string.diagnosis_result_text1) + wifiInfo.getSSID() + "\n\n");

        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null) {
            builder.append(getString(R.string.diagnosis_result_text2) + networkInfo.toString() + "\n\n");
        }

        builder.append(getString(R.string.diagnosis_result_text3) + wifiInfo.getBSSID() + "\n\n");
        builder.append(getString(R.string.diagnosis_result_text4) + wifiInfo.getHiddenSSID() + "\n\n");
        builder.append(getString(R.string.diagnosis_result_text5) + convertIp(wifiInfo.getIpAddress()) + "\n\n");
        builder.append(getString(R.string.diagnosis_result_text6) + wifiInfo.getLinkSpeed() + "Mbps\n\n");
        builder.append(getString(R.string.diagnosis_result_text7) + wifiInfo.getRssi() + "\n\n");
        builder.append(getString(R.string.diagnosis_result_text8) + wifiInfo.getSupplicantState().toString() + "\n\n");

        mDataBinding.tvDiagnosisResult.setText(builder.toString());
        mDataBinding.tvDiagnosisTip.setText(R.string.diagnosis_suc);
    }

    private static String convertIp(int ipValue) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i <= 3; i++) {
            builder.append((ipValue >> i * 8) & 0xFF);
            if (i < 3) {
                builder.append(".");
            }
        }
        return builder.toString();
    }

}